<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公告标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入公告标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公告状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择公告状态" clearable>
          <el-option
            v-for="dict in announcement_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建者" prop="createdBy">
        <el-input
          v-model="queryParams.createdBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          <el-button
          type="primary"
          @click="handleAdd"
          v-hasPermi="['manage:announcement:add']"
        >新增数据</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="announcementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"  />
      <el-table-column label="序号" width="60" align="center" prop="announcementId" />
      <el-table-column label="公告标题" align="center" prop="title" />
      <el-table-column label="公告类型"  width="100" align="center" prop="type">
        <template #default="scope">
          <dict-tag :options="announcement_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="公告状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="announcement_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createdBy" />
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope"> 
          <el-button link type="primary" @click="handleView(scope.row)" v-hasPermi="['manage:announcement:view']">查看公告内容</el-button>
          <el-button link type="primary"   @click="handleUpdate(scope.row)" v-hasPermi="['manage:announcement:edit']">修改</el-button>
          <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['manage:announcement:remove']">删除</el-button>
         
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改通知公告对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="announcementRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公告标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入公告标题" />
        </el-form-item>
        <el-form-item label="公告类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择公告类型">
            <el-option
              v-for="dict in announcement_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公告内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="公告状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in announcement_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="创建者" prop="createdBy">
          <el-input v-model="form.createdBy" placeholder="请输入创建者" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 查看通知公告详情对话框 -->
    <el-dialog :title="viewTitle" v-model="viewOpen" width="800px" append-to-body>
      <el-form :model="viewForm" label-width="80px">
        <el-form-item label="公告标题">
          <el-input v-model="viewForm.title" disabled />
        </el-form-item>
        <el-form-item label="创建者">
          <el-input v-model="viewForm.createdBy" disabled />
        </el-form-item>
        <el-form-item label="公告内容" class="centered-content">
          <div class="notice-content" v-html="viewForm.content" style="white-space: pre-wrap;"></div>
        </el-form-item>
      </el-form>
  <template #footer>
    <div class="dialog-footer">
      <el-button @click="viewOpen = false">关 闭</el-button>
    </div>
  </template>
</el-dialog>
  </div>
</template>

<script setup name="Announcement">
import { listAnnouncement, getAnnouncement, delAnnouncement, addAnnouncement, updateAnnouncement } from "@/api/manage/announcement";

const { proxy } = getCurrentInstance();
const { announcement_status, announcement_type } = proxy.useDict('announcement_status', 'announcement_type');

const announcementList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    status: null,
    createdBy: null,
  },
  rules: {
    title: [
      { required: true, message: "公告标题不能为空", trigger: "blur" }
    ],
    type: [
      { required: true, message: "公告类型不能为空", trigger: "change" }
    ],
    content: [
      { required: true, message: "公告内容不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "公告状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

const viewOpen = ref(false);
const viewTitle = ref("");
const viewForm = ref({});

/** 查询通知公告列表 */
function getList() {
  loading.value = true;
  listAnnouncement(queryParams.value).then(response => {
    announcementList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    announcementId: null,
    title: null,
    type: null,
    content: null,
    status: null,
    createdBy: null,
    createdTime: null,
    updatedBy: null,
    updatedTime: null,
    remarks: null
  };
  proxy.resetForm("announcementRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.announcementId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加通知公告";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _announcementId = row.announcementId || ids.value
  getAnnouncement(_announcementId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改通知公告";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["announcementRef"].validate(valid => {
    if (valid) {
      if (form.value.announcementId != null) {
        updateAnnouncement(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addAnnouncement(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _announcementIds = row.announcementId || ids.value;
  proxy.$modal.confirm('是否确认删除通知公告编号为"' + _announcementIds + '"的数据项？').then(function() {
    return delAnnouncement(_announcementIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/announcement/export', {
    ...queryParams.value
  }, `announcement_${new Date().getTime()}.xlsx`)
}

/** 查看按钮操作 */
function handleView(row) {
  getAnnouncement(row.announcementId).then(response => {
    viewForm.value = response.data;
    viewTitle.value = "查看通知公告详情";
    viewOpen.value = true;
  });
}

getList();
</script>
<style scoped>
.dialog-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.centered-content {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
}

.notice-content {
  background-color: #f9f9f9;
  border: 1px solid #ddd;
  padding: 15px;
  border-radius: 4px;
  min-height: 200px; /* 设置最小高度以确保内容有足够的显示空间 */
  white-space: pre-wrap; /* 保留换行符和空格 */
  word-break: break-word; /* 长单词或URL地址换行 */
  width: 100%; /* 使内容框占据整个宽度 */
}
</style>